package zero.tech.wsnet.node;

import java.util.Map;

import zero.tech.core.data.Resp;
import zero.tech.core.data.RespCode;
import zero.tech.wsnet.data.CMD;
import zero.tech.wsnet.data.Message;

public class NodeHeartBeatWork extends Thread {

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(10 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			try {
				Map<String, NodeClient> clients = NodeCacheClient.clients;
				for (NodeClient client : clients.values()) {
					if (client.isAlive()) {
						reqHeartBeat(client);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void reqHeartBeat(NodeClient client) {
		client.sendMessage(Message.build(CMD.COMMON_REQ_HEART_BEAT, Resp.build(RespCode.OK, "请求心跳")));
	}
}
